<?php
/**
 * Created by PhpStorm.
 * User: fengbob
 * Date: 2018/2/21
 * Time: 12:06
 */

namespace app\librarys;

use app\models\db\MiniPrograms;
use app\models\db\TUser;
use app\models\MiniProgramsExt;
use app\models\TUserExt;
use yii\db\Exception;

include("AopSdk.php");


class ZfbService
{

    public static function checkRsa($post,$app_id){
        try {
            $aop = new \AopClient;
//            var_dump( $app_id);
            $mini = MiniPrograms::findOne(['app_id'=>$app_id]);
//            var_dump($mini);
            $aop->alipayrsaPublicKey = $mini->public_key;

            $flag = $aop->rsaCheckV1($post, NULL, "RSA2");
        }catch (Exception $e){
            $flag = $e->getMessage();
        }finally{
//            $flag = 'in flag';
        }
        return $flag;
    }

    public static function getAuthToken($auth_code,$mini_programs){
        $c = new \AopClient();
        $c->gatewayUrl = "https://openapi.alipay.com/gateway.do";
//        $c->appId = "2018012702090304";
        $c->appId = $mini_programs->app_id;
//        $c->rsaPrivateKey = 'MIIEpAIBAAKCAQEAwIkBMM2dH9ATUR1I3og+53yS+vkyd2T/SGA23uK6/+tqoqIyYOvh34CDCyZQ4yy8afdEl7q3YJlSFi//+EOnBnlvuLXLTdgKK9go+Pnd5/jwS+iGWpVpJqc17Iuk5czZhfY54MaOQRpqBB0sgizscbMvwFjVNgy37rQxQGWLlUYTWVqtN5Zw2R9bU+V6Xc0rTG9a6eopnv4M9SvIqCb6io6olmWv4Dl6UBOwvR9IU6vcJNVRKjFs54JimvNXeMxVDsHHl2+/PefjzgqZwxIW8VE4bs+Bzh/xfAMGHRuZDvcRCIGHK4sUO0mLgycXkN8CCzC2UIAl6la2RXtS21EAPQIDAQABAoIBACLvuVuVs9E1/ItXeQWIS4HNxQ3tcg7gMMJ9UveWxL/zg+IG9IVJyC66Lw0y4rKWR4oi6DtwEh5hM9f61cPC4uf12SP3OPDjJtVV3/ltvap+LXxYsSh2vCHbbZmCrWVVipMYXuA+p0mA9YPRl2X3qN/o191bQTURkb6qy9UXmAB56i58NRjWozpooPRW+mjvYDWktSLQhXhep9LKQZu9EHyxfstetmC3u2pzlKPcCYrAFTxS9Fej+8JmCY0IrsGxuVicGiXo2C7Ch9mNeW+ClhX0MTXp/nLcQ7wlk3B8aMq2ozfqOH5Tx99R1WsqlQelTNQP9P0oBsmmpNIJScVGGYECgYEA3vaIPrA6MU8iukHU9yja0gk7TRdZ68UnxQqgj0QGhxBQ8p2LiTy25p5b3jKVb2TwhauDezwqkT1XayVQ9m0v0zb208BENV/zSz7YUn5jUjaWnTeNdqlhqbwi3k0Z0dR/DFoSm3nfobIG5rKi4jVOw7hKx5miIM0/stOUI/FgxqUCgYEA3RBHYDZWkEk8iQaHBkP6KO8OR1poG+eyoL+0B+wIxDWR749WSA+d3tHbqbzlsd2PrmG4zo4CTqd1tbFYaqNo8vBjeiBMNvQG4X3GJexVyMPOsAI7fdJHeyW2qQRHJ24lZhndj6Hn7yd9nLLlKYNLmtxlb648DSv0sGwAluCkN7kCgYEAy7CuB5c3kpSZRU+fH61TO07zvhn3LE2RZ5Gt3TRoQjvFt9/1+gpdjZJemz/bBUlR8swd8helS8qecmRE8twNNQihthGQPrLJsZRYTATq99CnCDoYIq/baRVXRX9qdyeKad5f4WoVw8LAS0InTEEnE+bpajAQzaa06sogLDX4E4ECgYAFoFhwtnFVksebpTLikfBYAl2Wq1JjA26kcLygRv2iIMrqEffcNamK9kRk+Y3PDRiPE6WkfDDK17BDQ5RAgpA1PrEJdrD55RQXwR+971nvliw4hpYFCckP9VHE03sMId2Jz7a35SMGDzc/Ob5jZBgUhO9//CtDGo9LvjP1oxoK4QKBgQCvnFJryUgL4HJ41fT6TIBITyirooLjBD+2VwjTHjgnT9OjGE0lJblINHhpdOvSXScY5aVvgNTHlZ06UJ0MK8coM62v9gNwnBaouOzNHu/OZaZucAq9z579kbUACQ5xGqqql4t0Vxc0YFE4gWh/goo/3C6tPMrZe4rSdPG7V/TmpQ==';
        $c->rsaPrivateKey = $mini_programs->priavte_key;
        $c->format = "json";
        $c->charset= "UTF-8";
        $c->signType= "RSA2";
        $c->alipayrsaPublicKey = $mini_programs->public_key;
//        $c->alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsIYjscISJXDIB58Te11oP6or0kDMjc1W9ehEeQ1NtcoZ06ujmAArjDnm7g3SUycU2D28d89ySNAsge5Kp5fVCBLAZYSlFrwB0iQaTcdjmPIH7J/ceUm0Ho/uoCor3/Bjzrx+37JTDlTcT3wSt/Sqw/O+JP9xc0RpNJr+6NeiTWdhn8eVty1gZvmCabdm+ln4WhXaM32tiiEqbt0ExMZZMORyYjw7LcJzExR/EEdVkeZi1UxLUbJr5IYjQZxP15S8U1jYLawjxizunF9QV6TZ2NhWb0ts2haCEUKTHHr8wNOGRZm7GjwkIGqDXmwRZQfnmapVl4EBVcIn8rx3ciClkwIDAQAB';
        $request = new \AlipaySystemOauthTokenRequest();
        $request->setGrantType("authorization_code");
        $request->setCode($auth_code);
        $result = $c->execute($request);
//        var_dump($result);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        try {
            $user_id = $result->$responseNode->user_id;
            $access_token = $result->$responseNode->access_token;
            $expires_in = $result->$responseNode->expires_in;
            $re_expires_in = $result->$responseNode->re_expires_in;
            $refresh_token = $result->$responseNode->refresh_token;

            $data['access_token'] = $access_token;
            $data['user_id'] = $user_id;
            $data['refresh_token'] = $refresh_token;
            $data['expires_in'] = $expires_in;
            $data['re_expires_in'] = $re_expires_in;
            $cache = \Yii::$app->cache;
            $cache->set(TUserExt::ZFB_LOGIN_STR.$data['user_id'], $data, 0);


        } catch (Exception $e) {
            return '';
        }

        //        echo 'result code:'.$resultCode.',';
//        if(!empty($resultCode)&&$resultCode == 10000){
//            echo "成功";
//        } else {
//            echo "失败".$responseNode;
//        }

        return $data;
    }

    public static function getPayOrderStr($bizcontent)
    {
        $aop = new \AopClient;
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = "2018012702090304";
        $aop->rsaPrivateKey = 'MIIEpA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ZRU+fH61TO07zvhn3LE2RZ5Gt3TRoQjvFt9/1+gpdjZJemz/bBUlR8swd8helS8qecmRE8twNNQihthGQPrLJsZRYTATq99CnCDoYIq/baRVXRX9qdyeKad5f4WoVw8LAS0InTEEnE+bpajAQzaa06sogLDX4E4ECgYAFoFhwtnFVksebpTLikfBYAl2Wq1JjA26kcLygRv2iIMrqEffcNamK9kRk+Y3PDRiPE6WkfDDK17BDQ5RAgpA1PrEJdrD55RQXwR+971nvliw4hpYFCckP9VHE03sMId2Jz7a35SMGDzc/Ob5jZBgUhO9//CtDGo9LvjP1oxoK4QKBgQCvnFJryUgL4HJ41fT6TIBITyirooLjBD+2VwjTHjgnT9OjGE0lJblINHhpdOvSXScY5aVvgNTHlZ06UJ0MK8coM62v9gNwnBaouOzNHu/OZaZucAq9z579kbUACQ5xGqqql4t0Vxc0YFE4gWh/goo/3C6tPMrZe4rSdPG7V/TmpQ==';
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        $aop->alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsIYjscISJXDIB58Te11oP6or0kDMjc1W9ehEeQ1NtcoZ06ujmAArjDnm7g3SUycU2D28d89ySNAsge5Kp5fVCBLAZYSlFrwB0iQaTcdjmPIH7J/ceUm0Ho/uoCor3/Bjzrx+37JTDlTcT3wSt/Sqw/O+JP9xc0RpNJr+6NeiTWdhn8eVty1gZvmCabdm+ln4WhXaM32tiiEqbt0ExMZZMORyYjw7LcJzExR/EEdVkeZi1UxLUbJr5IYjQZxP15S8U1jYLawjxizunF9QV6TZ2NhWb0ts2haCEUKTHHr8wNOGRZm7GjwkIGqDXmwRZQfnmapVl4EBVcIn8rx3ciClkwIDAQAB';
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request = new \AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数，这里只需要传入业务参数
//        $bizcontent = "{\"body\":\"我是测试数据\","
//            . "\"subject\": \"App支付测试\","
//            . "\"out_trade_no\": \"20170125test01\","
//            . "\"timeout_express\": \"30m\","
//            . "\"total_amount\": \"0.01\","
//            . "\"product_code\":\"QUICK_MSECURITY_PAY\""
//            . "}";
        $request->setNotifyUrl("http://zfapi.dorabox.com.cn/api/zfb-call-back");
        $request->setBizContent($bizcontent);
//这里和普通的接口调用不同，使用的是sdkExecute
        $response = $aop->sdkExecute($request);
//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
//        return htmlspecialchars($response);//就是orderString 可以直接给客户端请求，无需再做处理。
        return $response;
    }


}